//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.gui.fixed;

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import org.jpgolay.gui.date.DateComboBox;

public class LabelDateButton extends JPanel {

    public LabelDateButton() {
        jLabel = null;
        dateComboBox = null;
        date = "";
        jPanel = null;
        jButton = null;
        initialize();
    }

    private void initialize() {
        setLayout(new BorderLayout());
        setSize(306, 27);
        add(getJPanel(), "Center");
    }

    private JPanel getJPanel() {
        if (jPanel == null) {
            jPanel = new JPanel();
            FlowLayout flowLayout3 = new FlowLayout();
            jPanel.setLayout(flowLayout3);
            flowLayout3.setHgap(2);
            flowLayout3.setVgap(2);
            jPanel.add(getJLabel(), null);
            jPanel.add(getDateComboBox(), null);
            jPanel.add(getJButton(), null);
        }
        return jPanel;
    }

    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.setPreferredSize(new Dimension(50, 23));
            jButton.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/save.png")));
            jButton.setBorderPainted(false);
            jButton.setContentAreaFilled(false);
            jButton.setToolTipText("Grouper les BL s\351l\351ctionn\351s");
        }
        return jButton;
    }

    private JLabel getJLabel() {
        if (jLabel == null) {
            jLabel = new JLabel();
            jLabel.setText("N/A");
            jLabel.setPreferredSize(new Dimension(100, 23));
        }
        return jLabel;
    }

    private DateComboBox getDateComboBox() {
        if (dateComboBox == null) {
            dateComboBox = new DateComboBox();
            dateComboBox.setPreferredSize(new Dimension(150, 23));
            dateComboBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
            dateComboBox.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    String old = date;
                    date = (String) dateComboBox.getSelectedItem();
                    firePropertyChange("date", old, date);
                }
            });
        }
        return dateComboBox;
    }

    public void setLabel(String txt) {
        getJLabel().setText(txt);
    }

    public String getLabel() {
        return getJLabel().getText();
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        String old = date;
        this.date = date;
        firePropertyChange("date", old, date);
        dateComboBox.setModel(new DefaultComboBoxModel(new String[]{
                    date
                }));
    }

    public void setEnabledDateComboBox(boolean b) {
        getDateComboBox().setEnabled(b);
    }

    public boolean isEnabledDateComboBox() {
        return getDateComboBox().isEnabled();
    }

    public JButton getButton() {
        return getJButton();
    }
    private JLabel jLabel;
    private DateComboBox dateComboBox;
    private String date;
    private JPanel jPanel;
    private JButton jButton;
}
